package org.web.ssm.service.impl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.plugins.convert.TypeConverters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.web.ssm.dao.UserMapper;
import org.web.ssm.exception.BaseException;
import org.web.ssm.exception.UserException;
import org.web.ssm.model.Product;
import org.web.ssm.model.User;
import org.web.ssm.model.queryVo.BaseQuery;
import org.web.ssm.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by LUOLIANG on 2016/5/8.
 */

@Service("userService")
public class UserServiceImpl implements IUserService {
    private Logger logger = LogManager.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserByUsername(String username, String password) {
        User user = userMapper.selectByUsername(username, password);
        if (user != null) {
            user.setLoginstate(true);
            userMapper.updateUser(user);
        }

        return user;
    }

    @Override
    public List<User> getAll() {
        return userMapper.selectAll();
    }

    @Override
    public BaseQuery<User> getUserList(int pageSize, int pageCurrent) {
        BaseQuery<User> productQuery = new BaseQuery<>();
        productQuery.setPageSize(pageSize);
        productQuery.setMaxRow(pageSize * pageCurrent + 1);
        productQuery.setMinRow(pageSize * (pageCurrent - 1));
        productQuery.setCurrentPage(pageCurrent);
        productQuery.setResults(userMapper.getProductList(productQuery));
        productQuery.setTotalRow(userMapper.getCount(productQuery));
        int totalPage = productQuery.getTotalRow() % productQuery.getPageSize() == 0 ? productQuery.getTotalRow() / productQuery.getPageSize() : productQuery.getTotalRow() / productQuery.getPageSize() + 1;
        productQuery.setTotalPage(totalPage);
        return productQuery;
    }

    @Override
    public User getUserByOpenid(String openid) {
        return userMapper.selectByOpenid(openid);
    }

    public User getUserById(String userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    public void updateUser(User user) {
        try {
            int result = userMapper.updateUser(user);

            if (result != 1) {
                throw new UserException("更新用户失败");
            }
        } catch (UserException e) {
            throw e;
        } catch (Exception e1) {
            logger.error(e1.getMessage());
            throw new BaseException("更新用户操作异常：" + e1.getMessage());
        }
    }
    public void insertUser(User user) {
        try {
            user.setLoginstate(false);
            int result = userMapper.insertUser(user);

            if (result != 1) {
                throw new UserException("添加用户失败");
            }
        } catch (UserException e) {
            throw e;
        } catch (Exception e1) {
            logger.error(e1.getMessage());
            throw new BaseException("添加用户操作异常：" + e1.getMessage());
        }
    }

    public void deleteUser(String id) {
        userMapper.deleteUser(id);
    }
}
